package com.zgz.park.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zgz.park.common.config.Result;
import com.zgz.park.pojo.Admin;
import com.zgz.park.pojo.User;
import com.zgz.park.pojo.common.CommonPageReq;
import com.zgz.park.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author willie
 * @since 2022-12-10
 */
@CrossOrigin
@RestController
@RequestMapping("/park/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/getUserListByPage")
    public Result getUserListByPage(@RequestBody CommonPageReq queryData){
        IPage<User> userListByPage = userService.getUserListByPage(queryData);
        List<User> userList = userListByPage.getRecords();
        Map<String,Object> map = new HashMap<>();
        long total = userListByPage.getTotal();

        map.put("userList",userList);
        map.put("total",total);
        return Result.succ().data(map);
    }

    @GetMapping("/getUserById")
    public Result getUserById(@RequestParam("id") String id){
        Map<String,Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(id)) {
            User vo = userService.getUserById(id);
            map.put("vo",vo);
        }
        return Result.succ().data(map);
    }

    @PostMapping("/saveSubmit")
    public Result saveSubmit(@RequestBody Admin admin) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        boolean result = userService.saveSubmit(admin);
        if(result){
            return Result.succ().message("操作成功");

        }else{
            return Result.succ().message("操作失败");
        }
    }

    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam("id") String id){

        boolean result = userService.deleteById(id);
        if(result){
            return Result.succ().message("操作成功");

        }else{
            return Result.succ().message("操作失败");
        }

    }

}

